<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/index.js'
import { areaList } from "@vant/area-data";
import { showSuccessToast, Toast } from "vant";
import api from "@/api/api";

// 响应式数据
const field = reactive({
  id: "",
  name: "",
  mobile: "",
  consigneeAddressPath: "",
  detail: "",
  isDefault: false,
})

const isDefault = ref(false)
const area = ref("")
const showArea = ref(false)
const pickerValue = ref([]);
const addTitle = ref("新增收货地址")
const editTitle = ref("编辑收货地址")
const type = ref(0) // 新增 1编辑
const id = ref(-1)

// 路由和状态管理
const route = useRoute()
const router = useRouter()
const store = useUserStore()

// 获取地址列表
const addressList = computed(() => store.addressList)

// 方法定义
const onClickLeft = () => {
  router.back()
}

const onConfirm = ({ selectedValues, selectedOptions }) => {
  pickerValue.value = selectedValues.length
    ? selectedValues[selectedValues.length - 1]
    : '';
  showArea.value = false;
  area.value = selectedOptions.map((item) => item.text).join('/');
};

const submit = async () => {
  if (type.value == 0) {
    await api.user.addAddress(field);
  } else {
    await api.user.editAddress(field);
    if (isDefault.value) {
      await api.user.setDefaultAddress(field.id);
    }
  }
  showSuccessToast("操作成功")
  setTimeout(() => {
    router.back();
  }, 1000);
}

const getAddressInfo = async () => {
  let res = addressList.value.find((item) => item.id == id.value);
  // let res = await api.getAddressDetail(id.value);
  Object.assign(field, JSON.parse(JSON.stringify(res)));
  delete field.userId;
  area.value = field.consigneeAddressPath.replaceAll(",", " ");
  field.isDefault = !!res.isDefault;
}

// 生命周期钩子
onMounted(() => {
  type.value = route.query.type || 0;
  if (type.value == 1) {
    id.value = route.query.id;
    getAddressInfo();
  }
})
</script>

<template>
  <div class="address_detail">
    <van-nav-bar :title="type == 0 ? addTitle : editTitle" class="nav_bar" fixed left-arrow @click-left="onClickLeft" />
    <div class="form_1">
      <van-field v-model="field.name" input-align="right" :border="false" label="姓名" placeholder="请输入姓名" />
      <van-field v-model="field.mobile" input-align="right" label="手机号码" :border="false" placeholder="请输入电话" />
      <van-field v-model="area" input-align="right" :border="false" label="地区" readonly placeholder="请选择地区"
        @click="showArea = true" />
      <van-popup v-model:show="showArea" position="bottom">
        <van-area :area-list="areaList" :model-value="pickerValue" @confirm="onConfirm" @cancel="showArea = false" />
      </van-popup>
      <van-field v-model="field.detail" input-align="right" :border="false" label="详细地址" placeholder="请输入地址" />
    </div>
    <div class="form_2">
      <van-field name="switch" label="设为默认地址" input-align="right">
        <template #input>
          <van-switch v-model="field.isDefault" class="default_icon" inactive-color="#e9e9ea" />
        </template>
      </van-field>
    </div>
    <div class="bottom">
      <div class="btn" @click="submit">保存</div>
    </div>
  </div>
</template>



<style lang="less" scoped>
.address_detail {
  min-height: 100vh;
  background: url("https://zxcxappimg.oss-cn-hangzhou.aliyuncs.com//zxcxmall/2025/05/29/4431a33cd62f4efc9fc53a7606eee382.png") no-repeat;
  background-size: cover;
  background-attachment: fixed;
  background-position: center;
  box-sizing: border-box;
  padding: 200px 50px 150px 50px;

  :deep(.van-nav-bar__content) {
    height: 100px;
    margin: 25px auto;
  }

  :deep(.van-nav-bar__title) {
    font-size: 54px;
    font-weight: 500;
  }

  :deep(.van-ellipsis) {
    overflow: initial;
  }

  :deep(.van-nav-bar__arrow) {
    font-size: 60px;
  }

  :deep(.van-nav-bar__left) {
    left: 30px;
  }

  .form_1,
  .form_2 {
    padding: 40px 20px;
    background: #fff;
    border-radius: 10px;

    :deep(.van-cell) {
      font-size: 48px;
      padding: 50px;
    }
  }

  .form_2 {
    margin-top: 30px;

    .default_icon {
      width: 150px;
      height: 80px;
      border-radius: 80px;
    }

    :deep(.van-switch__node) {
      width: 80px;
      height: 80px;
    }

    :deep(.van-switch--on .van-switch__node) {
      transform: translateX(80px);
    }
  }

  .bottom {
    margin-top: 200px;
    width: 100%;
    padding: 0 100px;

    .btn {
      height: 150px;
      padding: 0 50px;
      background: #068ffb;
      border-radius: 999px;
      text-align: center;
      line-height: 150px;
      font-size: 60px;
      color: #fff;
      font-weight: bold;
    }
  }
}

:deep(.van-picker__toolbar) {
  height: 100px;
}

:deep(.van-picker__confirm) {
  font-size: 48px;
  padding: 20px 50px;
}

:deep(.van-picker__cancel) {
  font-size: 48px;
  padding: 20px 50px;
}

:deep(.van-picker-column) {
  font-size: 55px;
}
</style>
